package com.hhit.zhgd.service.impl

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.hhit.zhgd.dao.ZhgdDictionaryMapper
import com.hhit.zhgd.domain.entity.ZhgdDictionary
import com.hhit.zhgd.domain.response.BaseResp
import com.hhit.zhgd.service.ZhgdDictionaryService
import org.springframework.stereotype.Service

@Service("zhgdDictionaryService")
class ZhgdDictionaryServiceImpl : ZhgdDictionaryService, ServiceImpl<ZhgdDictionaryMapper, ZhgdDictionary>() {
    override fun getDictionaryPageInfo(keyWord: String?, page: Int, size: Int): IPage<ZhgdDictionary?>? {
        val queryWrapper = QueryWrapper<ZhgdDictionary>()
        if (keyWord?.isNotBlank()==true) {
            queryWrapper.like("code", keyWord)
                .or().like("parent_code", keyWord)
                .or().like("value", keyWord)
                .or().like("remark", keyWord)
        }
        val pageRequest =
            Page<ZhgdDictionary>(page.toLong(), size.toLong())
        return page(pageRequest, queryWrapper)
    }

    override fun getDictionaryByPCode(pCode: String?): List<ZhgdDictionary?>? {
        return if (pCode?.isNotBlank()==true) {
            list(QueryWrapper<ZhgdDictionary>().eq("parent_code", pCode))
        } else ArrayList<ZhgdDictionary>()
    }

    override fun addDictionary(zhgdDictionary: ZhgdDictionary?): BaseResp<Any?>? {
        if (zhgdDictionary?.parentCode?.isNotBlank() == true) {
            val byPcodeAndCodeList = this.list(
                QueryWrapper<ZhgdDictionary?>().eq("parent_code", zhgdDictionary?.parentCode)
                    .eq("code", zhgdDictionary?.code)
            )
            if (byPcodeAndCodeList?.isNotEmpty() == true) {
                return BaseResp.success("添加失败,该pCode和code已存在!")
            }
        }
        return if (save(zhgdDictionary)) BaseResp.success("添加成功") else BaseResp.fail("添加失败")
    }

    override fun editDictionary(zhgdDictionary: ZhgdDictionary?): BaseResp<Any?>? {
        val origindic = getById(zhgdDictionary?.id)
        if (zhgdDictionary?.parentCode?.isNotBlank() == true && origindic?.parentCode != zhgdDictionary?.parentCode) {
            val byPcodeList = this.list(
                QueryWrapper<ZhgdDictionary?>().eq("parent_code", zhgdDictionary?.parentCode)
            )
            if (byPcodeList?.isNotEmpty() == true) {
                return BaseResp.fail("修改失败,该pCode已存在!")
            }
        }
        if (origindic?.code != zhgdDictionary?.code) {
            val byPcodeAndCodeList = this.list(
                QueryWrapper<ZhgdDictionary?>().eq("parent_code", zhgdDictionary?.parentCode)
                    .eq("code", zhgdDictionary?.code)
            )
            if (byPcodeAndCodeList?.isNotEmpty() == true) {
                return BaseResp.fail("修改失败,该pCode和code已存在!")
            }
        }
        return if (updateById(zhgdDictionary)) BaseResp.success("修改成功!") else BaseResp.fail("修改失败!")
    }

    override fun delDictionary(id: Int?): BaseResp<Any?>? {
        val byId = getById(id)
        val list = this.list(
            QueryWrapper<ZhgdDictionary?>().eq("parent_code", byId?.code)
        )
        if (list?.isNotEmpty() == true) {
            return BaseResp.fail("删除失败!该code做为父节点还存在数据!")
        }
        return if (this.removeById(id)) BaseResp.success("删除成功!") else BaseResp.fail("删除失败!")
    }
}